package com.xzsoft.gdwz.services.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xzsoft.gdwz.ps.dao.LimitVendorDao;
import com.xzsoft.gdwz.ps.model.LimitVendor;
import com.xzsoft.gdwz.services.sendLimitVendorService;

@Transactional
@Service("sendLimitVendorService")
public class sendLimitVendorServiceImpl implements sendLimitVendorService {

	Logger logger = Logger.getLogger(sendLimitVendorServiceImpl.class);
	@Autowired
	private LimitVendorDao limitVendorDao;

	
	@Override
	public void saveInqVendor(JSONArray jsonArray) throws Exception {
		logger.info("限时报价供应商信息："+jsonArray+jsonArray.size());
		for(int i=0;i<jsonArray.size();i++){
			JSONObject map = (JSONObject)jsonArray.get(i);
//			LimitVendor lv=new LimitVendor();
//			//校验询价单和供应商信息
//			List<?> list = limitVendorDao.checkInqVendor((String)map.get("inqhid"), (String)map.get("vendorid"));
//			if(list.size()>0){
//				lv.setInqHId((String)map.get("inqhid"));
//				lv.setVendorId((String)map.get("vendorid"));
//				limitVendorDao.insertLimitVendor(lv);
//			}else{
//				throw new  Exception(JSONObject.fromObject(map).toString()+"-询价单或供应商信息错误！");
//			}
			try {
				saveAndcheckInqVendor(map);
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
				//保存日志
			}
		}
	}
	
	public void saveAndcheckInqVendor(JSONObject map) throws Exception{
		LimitVendor lv=new LimitVendor();
		//校验询价单和供应商信息
		List<?> list = limitVendorDao.checkInqVendor((String)map.get("inqhid"), (String)map.get("vendorid"));
		if(list.size()>0){
			lv.setInqHId((String)map.get("inqhid"));
			lv.setVendorId((String)map.get("vendorid"));
			limitVendorDao.insertLimitVendor(lv);
		}else{
			throw new  Exception(JSONObject.fromObject(map).toString()+"-询价单或供应商信息错误！");
		}
	}
}
